import os, sys
sys.path.append(os.getcwd())
from base.base_action import BaseAction
from selenium.webdriver.common.by import By

class LoginPage(BaseAction):

    _authorization_button = By.XPATH, "//*[@class='android.widget.Button' and @text = '允许']"
    _into_shop_button = By.XPATH, "//*[@resource-id = 'com.flyme.meizu.store:id/enter' and @index = '3' ]"
    _my_button = By.XPATH, "//*[@text = '我的' and @resource-id = 'com.flyme.meizu.store:id/nav_title']"
    _nologin_button = By.XPATH, "//*[@text = '未登录' and @resource-id = 'com.flyme.meizu.store:id/person_item_describe']"

    _username_button = By.XPATH, "//*[@resource-id = 'account']"
    _password_button = By.XPATH, "//*[@resource-id = 'password']"

    _login_button = By.XPATH, "//*[@resource-id = 'login' and @text = '登录']"

    _login_success_tips = By.XPATH, "//*[@resource-id = 'com.flyme.meizu.store:id/person_item_describe' and @index = '0']"
    _login_false_tips = By.XPATH, "//*[@resource-id = 'password-tips' and @index = '2']"

    _close_window_buton = By.XPATH, "//*[@resource-id = 'com.flyme.meizu.store:id/iv_close' and @index = '1']"


    def __init__(self, driver):
        BaseAction.__init__(self, driver)
        self.click_authorization()
        self.click_in_2_shop()
        self.close_window()
        self.click_my()
        self.click_nologin()



    def click_authorization(self):
        '''点击权限按钮'''
        self.click(self._authorization_button)

    def click_in_2_shop(self):
        '''点击进入商城按钮'''
        self.click(self._into_shop_button)

    def click_my(self):
        '''点击我的'''
        self.click(self._my_button)

    def click_nologin(self):
        '''点击未登录'''
        self.click(self._nologin_button)

    def input_username(self,username):
        '''输入用户名'''
        self.input_text(self._username_button, text = username)

    def input_password(self, password):
        '''输入密码'''
        self.input_text(self._password_button, text = password)

    def click_lohin(self):
        '''点击登录'''
        self.click(self._login_button)

    def login_success_is_exist_text(self,text):
        '''登录成功判断 用户名text是否存在'''
        self.find_text_is_exist(self._login_success_tips,text)

    def login_false_is_exist_text(self,text):
        '''登录不成功判断 用户名text是否存在'''
        self.find_text_is_exist(self._login_false_tips,text)

    def close_window(self,munber=5):
        self.is_exist_window_close(self._close_window_buton, munber)

    #
    # def is_exist_window_close(self, number = 5):
    #     '''关闭广告弹窗'''
    #     for i in range(number):
    #         try:
    #             self.click(self._close_window_buton)
    #         except Exception:
    #             print("无弹窗")
    #             break




